﻿#region Header

/*
Remote views based on linq
By

Pascal Hauser 
Dipl. Ing. in Informatik, Hochschule für Technik Rapperswil, 2006
Master Thesis, Hochschule für Technik Rapperswil, 2008-2010

and

Raphael Gfeller
Dipl. Ing. in Informatik, Hochschule für Technik Rapperswil, 2006
Master Thesis, Hochschule für Technik Rapperswil, 2008-2010

*/

#endregion

#region Usings

using System.Collections.Generic;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using InterLinq.Test.DH;
using InterLinq.Test.PD;
using Sebarf.Diagnostics.Interfaces;

#endregion

namespace InterLinq.Test.UI {
	/// <summary>
	/// Interaction logic for PagedProductsView.xaml
	/// </summary>
	public partial class PagedProductsView : UserControl {
		#region Public Methods

		private PagedProductsView() {
			InitializeComponent();
		}

		public PagedProductsView(LinqToSQLDataContext data)
			: this() {
			Check.ArgumentNotNull(data, "data");
			m_data = data;
			DoASearch();
		}

		#endregion

		#region Public Fields

		public const int CACHED_FACTOR = 8;
		public const int INIT = -1;
		public const int ViewSize = 15;

		#endregion

		#region Private Methods

		private object m_query = 1;

		private void Button_Click(object sender, RoutedEventArgs e) {
			DoASearch();
		}

		private void DoASearch() {
			m_searchValue = m_searchInput.Text;
			string searchValue = m_searchValue;
			m_pagedEntries = new AsnychPagedCollection<IProduct>(ViewSize, Dispatcher, GetElementCounts, GetElements);
			m_count.Content = m_pagedEntries.MaxElements;
			m_items.ItemsSource = m_pagedEntries.Elements;
			m_scrollBar.Maximum = m_pagedEntries.MaxElements;
			//m_scrollBar.Value = 0;
			m_scrollBar.SmallChange = 1;
			m_scrollBar.LargeChange = ViewSize;
			DH.SQL.Product x =
				(from p in m_data.Products.OrderBy(z => z.Name) where p.Name.Contains(searchValue) select p).First();
			//var y = (from p in m_data.Products.OrderBy(z=>z.Name)..Select((s, i) => new { S = s, Index = i }) where p.S.Name.Contains(searchValue) select p).First();
			if (x == null) {
				m_scrollBar.Value = 0;
			}
			else {
				int y = (from p in m_data.Products where p.Name.CompareTo(x.Name) <= 0 select new { p, p1 = 1 }).Count() - 1;
				m_scrollBar.Value = y;
			}
		}

		private int GetElementCounts() {
			string searchValue = m_searchValue;
			return (from p in m_data.Products orderby p.Name select p).Count();
		}

		private Dictionary<int, object> GetElements(int from, int to) {
			var toReturn = new Dictionary<int, object>();
			string searchValue = m_searchValue;
			int index = from;
			foreach (DH.SQL.Product entry in (from p in m_data.Products orderby p.Name select p).Skip(from).Take(to - from)) {
				toReturn.Add(index, entry);
				index++;
			}
			return toReturn;
		}

		private void ScrollBar_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e) {
			m_pagedEntries.View((int)m_scrollBar.Value);
		}

		#endregion

		#region Private Fields

		private readonly LinqToSQLDataContext m_data;
		private AsnychPagedCollection<IProduct> m_pagedEntries;
		private string m_searchValue;

		#endregion
	}
}